package com.simdo.g768.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.simdo.g768.Bean.IMSIState;
import com.simdo.g768.Bean.ImsiBean;
import com.simdo.g768.Listener.BeanClickListener;
import com.simdo.g768.R;
import com.simdo.g768.Util.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class WarningImsiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<ImsiBean> dataList;
    private final List<ImsiBean> uiDataList;
    private BeanClickListener mOnItemClickListener; //单击事件

    private final int red, color_1c1c1c;

    public WarningImsiAdapter(Context context, List<ImsiBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.uiDataList = new ArrayList<>();
        red = context.getResources().getColor(R.color.red);
        color_1c1c1c = context.getResources().getColor(R.color.color_1c1c1c);
    }

    @Override
    public int getItemCount() {
        return !uiDataList.isEmpty() ? uiDataList.size() : 1;//不同点4,也是关键点
    }

    @Override
    public int getItemViewType(int position) {
        if (uiDataList.isEmpty()) return -1;
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof EmptyHolder) return;
        final ViewHolder viewHolder = (ViewHolder) holder;

        final int pos = position;
        // 渲染数据
        ImsiBean item = uiDataList.get(pos);

        viewHolder.tv_name.setText(item.getNotes());
        viewHolder.tv_imsi.setText(item.getImsi());
        viewHolder.tv_arfcn.setText(item.getArfcn());
        viewHolder.tv_time.setText(DateUtil.formateTimeHMS(item.getLatestTime()));

        viewHolder.tv_imsi.setTextColor(item.getUpCount() > 1 ? red : color_1c1c1c);
        viewHolder.tv_imsi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener!=null) mOnItemClickListener.onItemClickListener(view, uiDataList.get(pos));
            }
        });
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup arg0, int arg1) {
        if (-1 == arg1)
            return new EmptyHolder(LayoutInflater.from(context).inflate(R.layout.item_empty, arg0, false));

        View item = LayoutInflater.from(context).inflate(R.layout.item_warning_imsi, arg0, false);
        return new ViewHolder(item);
    }

    /**
     * 暴露给外面的设置单击事件
     */
    public void setOnItemClickListener(BeanClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public synchronized void resetShowData() {
        List<ImsiBean> allList = new ArrayList<>(dataList);

        uiDataList.clear();
        int nowCount = 0;
        //排序  当前定位靠前， 黑名单紧次， 其他按时间排序靠后
        for (ImsiBean bean : allList) {
            if (bean.getUpCount() > 1) {
                uiDataList.add(nowCount, bean);
                nowCount += 1;
            } else {
                uiDataList.add(bean);
            }
        }

        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_name;
        TextView tv_imsi;
        TextView tv_arfcn;
        TextView tv_time;

        public ViewHolder(View itemView) {
            super(itemView);

            tv_name = itemView.findViewById(R.id.tv_name);
            tv_imsi = itemView.findViewById(R.id.tv_imsi);
            tv_arfcn = itemView.findViewById(R.id.tv_arfcn);
            tv_time = itemView.findViewById(R.id.tv_time);
        }
    }

    /**
     * 空数据显示的   不同点1
     */
    static class EmptyHolder extends RecyclerView.ViewHolder {

        public EmptyHolder(View itemView) {
            super(itemView);
        }

    }
}
